/**
 * 
 */
package com.repository.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.repository.dao.UserDAO;
import com.repository.exception.ApplicationException;
import com.repository.model.User;

/**
 * Service class for performing CRUD operations on {@link User}
 * 
 * @author lovkeshsingh
 * 
 */
@Service(value = "userRepositoryService")
public class UserRepositoryServiceImpl implements UserRepositoryService {

	@Autowired
	UserDAO userDAO;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.repository.service.UserRepositoryService#getUserList()
	 */
	@Override
	public List<User> getUserList() {

		return userDAO.getUsers();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.repository.service.UserRepositoryService#getUserByUserName(String
	 * userName)
	 */
	@Override
	public User getUserByUserName(String userName) {

		List<User> userList = userDAO
		.findByProperty(UserDAO.USERNAME, userName);

		if (userList != null && userList.size() > 0) {
			return userList.get(0);
		} else {
			return null;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.repository.service.UserRepositoryService#getUserById(long)
	 */
	@Override
	public User getUserById(long userId) {

		return userDAO.getUser(userId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.repository.service.UserRepositoryService#addUser(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public Long addUser(String userName, String password) {
		
		
		//check if a user with this userName alrady exists
		User existingUser = getExistingUser(null, userName);
		
		//user not found
		if(null == existingUser){

			User user = new User(userName, password);
			userDAO.saveOrUpdateUser(user);
			return user.getId();

		}else{
		
			throw new ApplicationException("User Already Exists");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.repository.service.UserRepositoryService#removeUser(java.lang.Long)
	 */
	@Override
	public void removeUser(Long id) {

		try {

			User user = userDAO.getUser(id);

			userDAO.removeUser(user);

		} catch (ApplicationException e) {

			throw e;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.repository.service.UserRepositoryService#updateUser(long,
	 * java.lang.String, java.lang.String)
	 */
	@Override
	public Long updateUser(Long id, String userName, String password) {

		User existingUser = getExistingUser(null, userName);
		
		//User with this userName already exists and it's not the current user
		if(null != existingUser && existingUser.getId().longValue() != id.longValue()){
			
			throw new ApplicationException("User with user name:"+userName+" already exists");
			
		}
		
		//get the user from persistant and update
		existingUser = getExistingUser(id, null);
		existingUser.setUserName(userName);
		existingUser.setPassword(password);
		
		userDAO.saveOrUpdateUser(existingUser);
		
		return existingUser.getId();
	}
	
	/**
	 * Fetch {@link User} from persistant store on the basis of id or userName
	 * @param userId
	 * @param userName
	 * @return {@link User} or null if user does not exist
	 */
	private User getExistingUser(Long userId, String userName){
		
		List<User> userList = null;
		
		if(null != userId && userId > 0){
			userList = userDAO.findByProperty(UserDAO.USERID, userId);
		}else{
			if(null != userName){
				userList = userDAO.findByProperty(UserDAO.USERNAME, userName);
			}
		}
		
		if(null != userList && userList.size() > 0){
			return userList.get(0);
		}else{
			return null;
		}
		
		
	}

}
